
import request from "../../../utils/request.ts";
import type {PermissionResponseDate, RoleData, RoleResponseData} from "./type.ts";

//@ts-ignore
enum API {
    //获取全部的职位，分页查询
    ALLROLE_URL = '/admin/acl/role/',
    //添加职位
    ADDROLE_URL = '/admin/acl/role/save',
    //更新职位
    UPDATEROLE_URL = '/admin/acl/role/update',
    //获取菜单和按钮数据
    ALLPERMISSION = '/admin/acl/permission/toAssign/',
    //给相应职位分配权限
    SETPERMISTION_URL = '/admin/acl/permission/doAssign',
}

//没有roleName就是所有数据的分页，有roleName就是模糊查询分页查询
export const reqAllRoleList = (currentPage: number, pageSize: number, roleName?: string) => {
    return request.get<any, RoleResponseData>(API.ALLROLE_URL + `${currentPage}/${pageSize}`, {params: {roleName}})
}

//添加或更新
export const reqAddOrUpdateRole = (data: RoleData) => {
    if (data.id) {
        return request.put<any, any>(API.UPDATEROLE_URL, data);
    } else {
        return request.post<any, any>(API.ADDROLE_URL, data);
    }
}

//获取菜单和权限按钮数据
export const reqAllMenuList = (roleId: number) => {
  return request.get<any, PermissionResponseDate>(API.ALLPERMISSION + roleId);
}

//给相应职位下发权限
export const reqSetPermission = (roleId: number, permissionId: number[]) => {
  return request.put<any, any>(API.SETPERMISTION_URL, {roleId, permissionId});
}